package com.brightfuture.chrms.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.brightfuture.chrms.common.core.util.R;
import com.brightfuture.chrms.common.log.annotation.SysLog;
import com.brightfuture.chrms.entity.AttendanceItemEntity;
import com.brightfuture.chrms.service.AttendanceItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.brightfuture.chrms.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 考勤项目
 *
 * @author Victor
 * @date 2024-02-05 22:43:03
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/attendanceItem" )
@Tag(description = "attendanceItem" , name = "考勤项目管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AttendanceItemController {

    private final  AttendanceItemService attendanceItemService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param attendanceItem 考勤项目
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('chrms_attendanceItem_view')" )
    public R getAttendanceItemPage(@ParameterObject Page page, @ParameterObject AttendanceItemEntity attendanceItem) {
        LambdaQueryWrapper<AttendanceItemEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(attendanceItemService.page(page, wrapper));
    }


    /**
     * 通过id查询考勤项目
     * @param itemId id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{itemId}" )
    @PreAuthorize("@pms.hasPermission('chrms_attendanceItem_view')" )
    public R getById(@PathVariable("itemId" ) Long itemId) {
        return R.ok(attendanceItemService.getById(itemId));
    }

    /**
     * 新增考勤项目
     * @param attendanceItem 考勤项目
     * @return R
     */
    @Operation(summary = "新增考勤项目" , description = "新增考勤项目" )
    @SysLog("新增考勤项目" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('chrms_attendanceItem_add')" )
    public R save(@RequestBody AttendanceItemEntity attendanceItem) {
        return R.ok(attendanceItemService.save(attendanceItem));
    }

    /**
     * 修改考勤项目
     * @param attendanceItem 考勤项目
     * @return R
     */
    @Operation(summary = "修改考勤项目" , description = "修改考勤项目" )
    @SysLog("修改考勤项目" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('chrms_attendanceItem_edit')" )
    public R updateById(@RequestBody AttendanceItemEntity attendanceItem) {
        return R.ok(attendanceItemService.updateById(attendanceItem));
    }

    /**
     * 通过id删除考勤项目
     * @param ids itemId列表
     * @return R
     */
    @Operation(summary = "通过id删除考勤项目" , description = "通过id删除考勤项目" )
    @SysLog("通过id删除考勤项目" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('chrms_attendanceItem_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(attendanceItemService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param attendanceItem 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('chrms_attendanceItem_export')" )
    public List<AttendanceItemEntity> export(AttendanceItemEntity attendanceItem,Long[] ids) {
        return attendanceItemService.list(Wrappers.lambdaQuery(attendanceItem).in(ArrayUtil.isNotEmpty(ids), AttendanceItemEntity::getItemId, ids));
    }
}